package com.qikecn.api.common;

import java.io.File;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.qikecn.api.model._MappingKit;

import hlinfo.api.wxlogin.WXLoginConfig;

public class CommonConfig extends JFinalConfig {

	@Override
	public void configConstant(Constants me) {
		// 加载少量必要配置，随后可用PropKit.get(...)获取值
		PropKit.use("config.properties");
		boolean devMode = PropKit.getBoolean("devMode", false);
		me.setDevMode(devMode);
		me.setMaxPostSize(10000000);
		me.setEncoding("UTF-8");
		
		Contents.SMS_KEY = PropKit.get("sms_key", "");
		Contents.SMS_URL = PropKit.get("sms_url", "");
		Contents.ROOT_PATH = PathKit.getWebRootPath() + File.separator; //PropKit.get("ROOT_PATH", "");//PathKit.getWebRootPath() + File.separator;   更换
		Contents.BASE_URL = PropKit.get("base_url", "");
		//Contents.SESSION_TIMEOUT = PropKit.getInt("session_timeout", new Integer(86400));
		Contents.MEMCACHED_SERVER = PropKit.get("memcached_server", "");
		
		System.out.println("====print config begin===");
		System.out.println("devMode="+devMode);
		System.out.println("base_url="+Contents.BASE_URL);
		System.out.println("root_path="+Contents.ROOT_PATH);
		System.out.println("sms_key="+Contents.SMS_KEY);
		System.out.println("sms_url="+Contents.SMS_URL);
		System.out.println("memcached_server="+Contents.MEMCACHED_SERVER);
		System.out.println("====print config end=====");
		
		//初始化环信配置
		//EasemobApi.init();
		//talk聊天
		//TalkApi.init();
		//TalkApi.debug = true;
		
		
	}
	
	@Override
	public void afterJFinalStart() {
		// TODO Auto-generated method stub
		super.afterJFinalStart();
		
		//微信授权登录
		WXLoginConfig u=new WXLoginConfig();
		System.out.println("appid="+u.appid);
		ApiConfig ac = new ApiConfig();
	     // 配置微信 API 相关参数
	     ac.setToken("");
	     ac.setAppId(u.appid);
	     ac.setAppSecret(u.appSecret);
	     
	     	/*
	         *  是否对消息进行加密，对应于微信平台的消息加解密方式：
	         *  1：true进行加密且必须配置 encodingAesKey
	         *  2：false采用明文模式，同时也支持混合模式
	         */
	        ac.setEncryptMessage(PropKit.getBoolean("encryptMessage", false));
	        //ac.setEncodingAesKey(PropKit.get("encodingAesKey", "setting it in config file"));

	        /**
	         * 多个公众号时，重复调用ApiConfigKit.putApiConfig(ac)依次添加即可，第一个添加的是默认。
	         */
	        ApiConfigKit.putApiConfig(ac);

	     
	}

	@Override
	public void configRoute(Routes me) {
		me.add(new CommonRoute());

	}
	
	//C3P0 链接池配置
	public static C3p0Plugin createC3p0Plugin() {
		return new C3p0Plugin(PropKit.get("jdbc.url"), PropKit.get("jdbc.username"), PropKit.get("jdbc.password"), 
				PropKit.get("jdbc.driverclass"), Integer.parseInt(PropKit.get("jdbc.maxpoolsize")), Integer.parseInt(PropKit.get("jdbc.minpoolsize")), Integer.parseInt(PropKit.get("jdbc.initpoolsize")), Integer.parseInt(PropKit.get("jdbc.maxidletime")), 2);
	}
	
	@Override
	public void configPlugin(Plugins me) {
		// 配置C3p0数据库连接池插件
		C3p0Plugin C3p0Plugin = createC3p0Plugin();
		me.add(C3p0Plugin);
		
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(C3p0Plugin);
		arp.setShowSql(true);//这句话就是ShowSql
		me.add(arp);
		
		// 所有配置在 MappingKit 中搞定
		_MappingKit.mapping(arp);
		
		//配置缓存插件  
        me.add(new EhCachePlugin());  

	}

	@Override
	public void configInterceptor(Interceptors me) {
		// TODO Auto-generated method stub
		me.add(new Interceptor() {
			@Override
			public void intercept(Invocation inv) {
				// TODO Auto-generated method stub
				inv.getController().getResponse().setHeader("Access-Control-Allow-Origin", "*");
				inv.invoke();
			}
		});
	}

	@Override
	public void configHandler(Handlers me) {
		// TODO Auto-generated method stub
		
	}

}
